<template>
  <a-modal
    title="客户(商圈)管理"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客户名称">
          <a-input :disabled="true" placeholder="客户名称"
                   v-decorator="[ 'name', {rules: [] }]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="通道配置"
          v-action:other
        >
          <div v-for="(item,i) in items">
            <a-select style="width: 35%" placeholder="请选择" v-model="item.id">
              <a-select-option v-for="pw in passagewayList" :key="pw.id">{{pw.name}}</a-select-option>
            </a-select>
            <span style="margin-left: 10px">费率：</span>
            <a-input style="width: 38%" addonAfter="3位小数" v-model="item.rate"/>&nbsp;
            <a-button style="width: 15%" type="danger" @click="onDel(i)">删除</a-button>
          </div>
          <a-button @click="onAdd" type="primary" style="margin-top: 6px">添加</a-button>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { selectAllPassageway, selectWays, addPassageway } from '@/api/mqpay/passageway'

  export default {
    data () {
      return {
        id: null,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        isDisabled: false,
        passagewayList: [],
        items: [],

        form: this.$form.createForm(this)
      }
    },
    mounted () {
    },
    methods: {
      edit (record) {
        this.id = record.id
        this.visible = true
        selectAllPassageway(record.code).then(res => {
          if (res.code === 200) {
            this.passagewayList = res.result
            selectWays(record.id).then(res => {
              this.items = res.result
            })
          }
        })

        const { form: { setFieldsValue } } = this
        this.$nextTick(() => {
          let mdl = {}
          mdl.name = record.name
          setFieldsValue(mdl)
        })

      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.id = this.id
            values.ways = this.items
            addPassageway(values).then((res) => {
              this.confirmLoading = false
              if (res.code === 200) {
                this.handleCancel()
                this.$emit('ok')
              }
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.visible = false
        this.isDisabled = false
        this.form.resetFields()
        this.id = null
        this.items = []
      },
      onAdd () {
        let cope = {}
        this.items.push(cope)
        // console.log(this.items);
      },
      onDel: function (index) {
        this.items.splice(index, 1)
      }
    }
  }
</script>
